/*
 * DBManager.cpp
 *
 *  Created on: Sep 18, 2016
 *      Author: root
 */

#include "yDBManager.h"
#include "../../third_part/xml/tinyxml2.h"
ConnPool* DBManager::pool = NULL;
using namespace tinyxml2;
void DBManager::init() {
	XMLDocument doc;
	doc.LoadFile("conf/global.xml");
	const char* url = doc.RootElement()->FirstChildElement("database")->FirstChildElement("url")->GetText();
	const char* userName = doc.RootElement()->FirstChildElement("database")->FirstChildElement("user")->GetText();
	const char* password = doc.RootElement()->FirstChildElement("database")->FirstChildElement("password")->GetText();
	pool = new ConnPool(url, userName, password, 20);
	Connection* conn = pool->getConnection();
	PreparedStatement* state = conn->prepareStatement("SELECT * FROM t_u_mail");
	ResultSet *result = state->executeQuery();
	while (result->next()) {
		printf("id %d \n", result->getInt(1));
	}
}

void DBManager::destory() {
	delete (pool);
}

Connection* DBManager::getConnection() {
	return pool->getConnection();
}

void DBManager::closeConnection(Connection* conn) {
	pool->releaseConnection(conn);
}

void DBManager::closeConnection(Connection* conn, PreparedStatement* state, ResultSet* result) {
	pool->releaseConnection(conn);
	if (state)
		state->close();
	if (result)
		result->close();
}
